package com.mzpai.web.controller;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gexin.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.model.StoreUserRemark;
import com.mzpai.product.model.SystemStoreDdfBill;
import com.mzpai.product.model.User;
import com.mzpai.product.model.UserAddress;
import com.mzpai.product.model.WechatUser;
import com.mzpai.product.service.StoreDdfBillService;
import com.mzpai.product.service.StoreUserRemarkService;
import com.mzpai.product.service.UserAddressService;
import com.mzpai.product.service.UserBindStoreService;
import com.mzpai.product.service.UserPetService;
import com.mzpai.product.service.UserService;
import com.mzpai.product.service.WechatUserService;
import com.mzpai.product.vo.DeductGoldRechargeVo;
import com.mzpai.product.vo.DeducteGoldVo;
import com.mzpai.product.vo.StoreUserQueryVo;
import com.mzpai.product.vo.UserAddressVo;
import com.mzpai.product.vo.UserBindStoreVo;
import com.mzpai.product.vo.UserInfoVo;
import com.mzpai.product.vo.UserPetAndCateVo;
import com.mzpai.product.vo.UserReqVo;
import com.mzpai.util.ArithUtil;
import com.mzpai.util.DateUtil;
import com.mzpai.util.StringUtils;
import com.mzpai.vo.ResponseVo;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 *
 * 会员信息查询
 *
 */
@Api(value = "用户信息接口")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired 
	private UserService userService;
	@Autowired 
	private UserAddressService userAddressService;
	@Autowired
	private StoreDdfBillService storeDdfBillService;
	
	@Autowired
	private UserBindStoreService userBindStoreService;
	
	@Autowired
	private UserPetService userPetService;
	
	@Autowired
	private WechatUserService wechatUserService;
	
	@Autowired 
	private StoreUserRemarkService storeUserRemarkService;
	
	
	private static Logger log = LoggerFactory.getLogger(UserController.class);
	
	
	@ApiOperation(value = "获取会员信息")
    @RequestMapping(value = "/queryUserInfo",method = {RequestMethod. POST ,RequestMethod. GET})
    public ResponseVo<UserInfoVo> queryUserInfo(
    		@ApiParam(required = true, name = "queryUserStr", value = "入参为queryUserStr")
    		@RequestParam(value = "queryUserStr", required = true) String queryUserStr){
		
		log.info("msg1=开始获取会员信息,,method=queryUserInfo,,reqVo={}",queryUserStr);
		UserReqVo userReqVo = JSONObject.parseObject(queryUserStr, UserReqVo.class);
		
		Integer storeId=userReqVo.getStoreId();
		String userEncryptData=userReqVo.getUserEncryptData();
		
		
//		Integer qrTime=userReqVo.getCurrentDateTime();
//		String signData=userReqVo.getUserSignData();
		
		
		if(storeId==null){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		/**
		 * 业务逻辑
		 * 1.首先根据user_text查询用户表密文，可以获得用户信息 
		 * 2.如果用户信息存在，则取出用户表的密钥，将用户密文+二维码生成时间+用户密钥重新生成MD5与签名对比
		 * 3.如果签名对比无误，表示二维码有效
		 * 4.然后从配置表获取二维码有效期，比如10分钟有效期 如果当前时间超出有效期则返回二维码失效，需要冲洗你刷新二维码
		 * 5.以上全部通过，表示二维码完全有效
		 */
		//默认30分钟失效
//		Integer expiryMin=30;
//		SystemSetting systemSetting = systemSettingService.getSettingByCode("user_qr_expirydate");
//		if(systemSetting != null) {
//			String expieyDateTime=systemSetting.getValue();
//			expiryMin=Integer.valueOf(expieyDateTime);
//		}
//		Date qrDate=new Date(qrTime*1000l);
//		Date afterQrDate=DateUtil.addMinutes(qrDate, expiryMin);
		//会员二维码时效
		/*if(afterQrDate.compareTo(new Date())<0){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.QR_DATE_EXPIRY.getCode());
			resVo.setMessage(ProductResExceptionCode.QR_DATE_EXPIRY.getMessage());
			return resVo;
		}*/
		
		User user=userService.queryByUserText(userEncryptData);
		if(user==null){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.USER_INFO_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_INFO_NULL.getMessage());
			return resVo;
		}
		
		if(StringUtils.isNullOrBlank(userEncryptData)){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.USER_ENCRYPT_DATA_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_ENCRYPT_DATA_NULL.getMessage());
			return resVo;
		}
		Integer userId=user.getUid();
//		String secretKey=user.getSecretKey();
//		String userText=user.getUserText();
		String nickName=user.getNickname();
		String avatar=user.getAvatar();
		
		//生产md5进行比对
//		String userMd5Str=StringUtils.getMD5(userText+qrTime+secretKey);
		/*if(!userMd5Str.equals(signData)){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.USER_QR_SIGNDATE_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_QR_SIGNDATE_ERROR.getMessage());
			return resVo;
		}*/
		
		//可以抵扣京总额
		BigDecimal balanceTotal=getTotalDeducteGold(storeId, userId);
		
		//查询用户在门店下可以抵扣金总额
//		Map<String,Object> ddfBillMap=new HashMap<String,Object>();
//		ddfBillMap.put("storeId", storeId);
//		ddfBillMap.put("userId", userId);
//		List<SystemStoreDdfBill> storeDdfBillList=storeDdfBillService.getEffectiveStoreDdfBillList(ddfBillMap);
		
	/*	if(storeDdfBillList!=null && !storeDdfBillList.isEmpty()){
			Double blaceTotalValue=0d;
			for(SystemStoreDdfBill storeDdfBill:storeDdfBillList){
				//发放总金额
				BigDecimal totalAmount=storeDdfBill.getTotalAmount();
				//已使用总金额
				BigDecimal usedAmount=storeDdfBill.getUsedAmount();
				//余额
				BigDecimal balance=storeDdfBill.getBalance();
				//防止 使用总额大于发放总额
				if(totalAmount.doubleValue()>usedAmount.doubleValue()){
					Double balanceValue=balance.doubleValue();
					blaceTotalValue+=balanceValue;
				}
			}
			balanceTotal=new BigDecimal(blaceTotalValue);
		}else{
			balanceTotal=new BigDecimal(0);
		}*/
		
		UserInfoVo userInfoVo = new UserInfoVo();
		userInfoVo.setDeducteGold(balanceTotal);
		userInfoVo.setUserNickname(nickName);
		userInfoVo.setUserAvatar(avatar);
		
		ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
		resVo.setResult(userInfoVo);
		
	    return resVo;
	}
	
		
	@ApiOperation(value = "单表查询会员信息列表")
    @RequestMapping(value = "/queryOnlyUserListByPage",method = {RequestMethod. POST ,RequestMethod. GET })
    public ResponseVo<PageInfo<UserInfoVo>> queryOnlyUserListByPage(
    		@ApiParam(required = true, name = "queryUserListByPageStr", value = "入参为queryUserListByPageStr")
    		@RequestParam(value = "queryUserListByPageStr", required = true) String queryUserListByPageStr){
			
			
		log.info("msg1=根据单表查询会员信息列表,,method=queryOnlyUserListByPage,,reqVo={}",queryUserListByPageStr);
		StoreUserQueryVo userReqVo = JSONObject.parseObject(queryUserListByPageStr, StoreUserQueryVo.class);
		
		Integer storeId=userReqVo.getStoreId();
		Integer pageIndex=userReqVo.getPageIndex();
		Integer pageSize=userReqVo.getPageSize();
		String queryConditions=userReqVo.getQueryConditions();
			
		
		if(storeId==null){
			ResponseVo<PageInfo<UserInfoVo>> resVo=new ResponseVo<PageInfo<UserInfoVo>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		if(StringUtils.isNullOrBlank(queryConditions)){
			ResponseVo<PageInfo<UserInfoVo>> resVo=new ResponseVo<PageInfo<UserInfoVo>>();
			resVo.setCode(ProductResExceptionCode.NICKNAME_PHONE_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.NICKNAME_PHONE_NULL.getMessage());
			return resVo;
		}
		
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("queryConditions", queryConditions);
		PageInfo<UserInfoVo> userInfoVoPage=userService.getUserListByPage(queryMap, pageIndex, pageSize);
		
		List<UserInfoVo> listUserInfoVo=userInfoVoPage.getList();
		if(listUserInfoVo!=null && !listUserInfoVo.isEmpty()){
			for(UserInfoVo user:listUserInfoVo){
				Integer userId=user.getUserId();
				//可以抵扣京总额
				BigDecimal balanceTotal=getTotalDeducteGold(storeId, userId);
				user.setDeducteGold(balanceTotal);
			}
		}
		ResponseVo<PageInfo<UserInfoVo>> resVo=new ResponseVo<PageInfo<UserInfoVo>>();
		resVo.setResult(userInfoVoPage);
	    return resVo;
	}
	
	
	@ApiOperation(value = "抵扣金充值")
    @RequestMapping(value = "/deductGoldRecharge",method = {RequestMethod. POST ,RequestMethod. GET})
    public ResponseVo<Boolean> deductGoldRecharge(
    		@ApiParam(required = true, name = "deductGoldRechargeStr", value = "入参为deductGoldRecharge")
    		@RequestParam(value = "deductGoldRechargeStr", required = true) String deductGoldRechargeStr){
		
		log.info("msg1=开始获取会员信息,,method=deductGoldRechargeStr,,reqVo={}",deductGoldRechargeStr);
		DeductGoldRechargeVo deductGoldRechargeVo = JSONObject.parseObject(deductGoldRechargeStr, DeductGoldRechargeVo.class);
		
		Integer storeId=deductGoldRechargeVo.getStoreId();
		String userText=deductGoldRechargeVo.getUserText();
		BigDecimal totalMoney = deductGoldRechargeVo.getTotalMoney();
		
		if(storeId==null){
			ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		
		if(totalMoney==null || totalMoney.doubleValue()<=0){
			ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
			resVo.setCode(ProductResExceptionCode.LESS_THAN_ZERO_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.LESS_THAN_ZERO_ERROR.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		
		if(StringUtils.isBlank(userText)){
			ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
			resVo.setCode(ProductResExceptionCode.USER_ENCRYPT_DATA_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_ENCRYPT_DATA_NULL.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		User user=userService.queryByUserText(userText);
		if(user==null){
			ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
			resVo.setCode(ProductResExceptionCode.USER_INFO_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_INFO_NULL.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		Integer userId=user.getUid();
		//进行充值
		userService.addDeductGoldRecharge(deductGoldRechargeVo, userId);
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		resVo.setResult(true);
	    return resVo;
	}
	
	
	/**
	 * 
	 * @param queryProductByPageStr
	 * @return
	 */
	@ApiOperation(value = "根据条件分页查询门店绑定会员列表")
    @RequestMapping(value = "/queryUserListByPage",method = {RequestMethod. POST ,RequestMethod. GET })
    public ResponseVo<PageInfo<UserBindStoreVo>> queryUserListByPage(
    		@ApiParam(required = true, name = "queryUserListByPageStr", value = "入参为queryUserListByPageStr")
    		@RequestParam(value = "queryUserListByPageStr", required = true) String queryUserListByPageStr){
		
		log.info("msg1=根据条件分页查询门店绑定会员列表,,method=queryUserListByPage,,reqVo={}",queryUserListByPageStr);
		StoreUserQueryVo userReqVo = JSONObject.parseObject(queryUserListByPageStr, StoreUserQueryVo.class);
		
		Integer storeId=userReqVo.getStoreId();
		Integer pageIndex=userReqVo.getPageIndex();
		Integer pageSize=userReqVo.getPageSize();
		String queryConditions=userReqVo.getQueryConditions();
		
		if(storeId==null){
			ResponseVo<PageInfo<UserBindStoreVo>> resVo=new ResponseVo<PageInfo<UserBindStoreVo>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("storeId", storeId);
		if(!StringUtils.isNullOrBlank(queryConditions)){
			queryMap.put("queryConditions", queryConditions);
		}
		PageInfo<UserBindStoreVo> userBindStoreVoPage=userBindStoreService.getUserBindStoreListByPage(queryMap, pageIndex, pageSize);
		
		List<UserBindStoreVo> userBindStoreList= userBindStoreVoPage.getList();
		
		if(userBindStoreList!=null && !userBindStoreList.isEmpty()){
			for(UserBindStoreVo userBindStoreVo:userBindStoreList){
				Integer userId=userBindStoreVo.getUserId();
				//查询 用户门店备注关系表
				Map<String,Object> queryRemarkMap=new HashMap<String,Object>();
				queryRemarkMap.put("storeId", storeId);
				queryRemarkMap.put("userId", userId);
				List<StoreUserRemark> storeUserRemarkList=storeUserRemarkService.getList(queryRemarkMap);
				StoreUserRemark storeUserRemark=null;
				if(storeUserRemarkList!=null && !storeUserRemarkList.isEmpty()){
					storeUserRemark=storeUserRemarkList.get(0);
				}
				if(storeUserRemark!=null){
					userBindStoreVo.setRemark(storeUserRemark.getRemark());
				}
				//可以抵扣京总额
				BigDecimal balanceTotal=getTotalDeducteGold(storeId, userId);
				userBindStoreVo.setDeducteGold(String.valueOf(balanceTotal.doubleValue()));
			}
		}
		ResponseVo<PageInfo<UserBindStoreVo>> resVo=new ResponseVo<PageInfo<UserBindStoreVo>>();
		resVo.setResult(userBindStoreVoPage);
		log.info("msg1=结束根据条件分页查询门店绑定会员列表,,method=queryUserListByPage");
    	return resVo;
    }



	private BigDecimal getTotalDeducteGold(Integer storeId, Integer userId) {
		BigDecimal balanceTotal=null;
		//查询用户在门店下可以抵扣金总额
		Map<String,Object> ddfBillMap=new HashMap<String,Object>();
		ddfBillMap.put("storeId", storeId);
		ddfBillMap.put("userId", userId);
		List<SystemStoreDdfBill> storeDdfBillList=storeDdfBillService.getEffectiveStoreDdfBillList(ddfBillMap);
		if(storeDdfBillList!=null && !storeDdfBillList.isEmpty()){
			double blaceTotalValue=0.00d;
			for(SystemStoreDdfBill storeDdfBill:storeDdfBillList){
				//发放总金额
				BigDecimal totalAmount=storeDdfBill.getTotalAmount();
				//已使用总金额
				BigDecimal usedAmount=storeDdfBill.getUsedAmount();
				//余额
				BigDecimal balance=storeDdfBill.getBalance();
				//防止 使用总额大于发放总额
				if(totalAmount.doubleValue()>usedAmount.doubleValue()){
					Double balanceValue=balance.doubleValue();
					blaceTotalValue=ArithUtil.add(blaceTotalValue,balanceValue);
				}
			}
			balanceTotal=new BigDecimal(blaceTotalValue);
		}else{
			balanceTotal=new BigDecimal(0);
		}
		return balanceTotal;
	}
	
	
	
	
	@ApiOperation(value = "查询用户宠物档案")
	@RequestMapping(value = "/queryUserPetList",method = {RequestMethod. POST ,RequestMethod. GET})
	public ResponseVo<List<UserPetAndCateVo>> queryUserPetList(
			@ApiParam(required = true, name = "userId", value = "入参为用户id")
			@RequestParam(value = "userId", required = true) Integer userId) throws Exception {
	
		logger.info("msg1=开始查询用户档案,,method=queryUserPetList,,userId={}",userId);
		
		if(userId==null){
			ResponseVo<List<UserPetAndCateVo>> resVo=new ResponseVo<List<UserPetAndCateVo>>();
			resVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resVo;
		}
		
		List<UserPetAndCateVo> userPetList=userPetService.getUserPetAndCateListByUserId(userId);
		
		ResponseVo<List<UserPetAndCateVo>> res = new ResponseVo<List<UserPetAndCateVo>>();
		res.setResult(userPetList);
		logger.info("msg1=结束查询用户档案,,method=queryUserPetList");
		return res;
	}
	
	
	
	@ApiOperation(value = "获取会员详细信息")
    @RequestMapping(value = "/queryUserDetail",method = {RequestMethod. POST ,RequestMethod. GET})
    public ResponseVo<UserInfoVo> queryUserDetail(@RequestParam(value = "userId", required = true) Integer userId,
    		@RequestParam(value = "storeId", required = true) Integer storeId){
		
		log.info("msg1=获取会员详细信息,,method=queryUserDetail,,userId={}",userId);
		if(userId==null){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resVo;
		}
		User user=userService.queryByUserId(userId);
		if(user==null){
			ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
			resVo.setCode(ProductResExceptionCode.USER_INFO_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_INFO_NULL.getMessage());
			return resVo;
		}
		
		//可以抵扣京总额
		BigDecimal balanceTotal=getTotalDeducteGold(storeId, userId);
		
		WechatUser wechatUser=wechatUserService.queryByPrimaryId(userId);
		
		String nickName=user.getNickname();
		String avatar=user.getAvatar();
		String phone=user.getPhone();
		
		UserInfoVo userInfoVo = new UserInfoVo();
		userInfoVo.setUserNickname(nickName);
		userInfoVo.setUserAvatar(avatar);
		userInfoVo.setPhone(phone);
		userInfoVo.setDeducteGold(balanceTotal);
		
		if(wechatUser!=null){
			Integer sex=wechatUser.getSex();
			String unionid=wechatUser.getUnionid();
			if(sex!=null && sex.equals(1)){
				userInfoVo.setSex("男");
			}else if(sex!=null && sex.equals(2)){
				userInfoVo.setSex("女");
			}else{
				userInfoVo.setSex("未知");
			}
			userInfoVo.setWeChatId(unionid);
		}
		
		ResponseVo<UserInfoVo> resVo=new ResponseVo<UserInfoVo>();
		resVo.setResult(userInfoVo);
	    return resVo;
	}
	/***
	 * 获取用户地址
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据用户id, 获取用户地址信息")
	@RequestMapping(value = "/queryUserAddressList")
	public ResponseVo<List<UserAddressVo>> queryUserAddressList(@ApiParam(required = true, name = "userId", value = "入参为用户id")
			@RequestParam(value = "userId", required = true) Integer userId) throws Exception {
	
		logger.info("msg1=开始查询用户档案,,method=queryUserAddressList,,userId={}",userId);
		
		if(userId==null){
			ResponseVo<List<UserAddressVo>> resVo=new ResponseVo<List<UserAddressVo>>();
			resVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resVo;
		}
		ResponseVo<List<UserAddressVo>> res = new ResponseVo<List<UserAddressVo>>();
		List<UserAddressVo> voList = new LinkedList<UserAddressVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", userId);
		List<UserAddress> list = userAddressService.getList(map);
		if(!list.isEmpty()) {
			for(UserAddress c : list) {
				UserAddressVo vo = new UserAddressVo();
				BeanUtil.copyProperties(c, vo);
				voList.add(vo);
			}
		}
		res.setCode(200);
		res.setResult(voList);
		return res;
	}
	
	
	@ApiOperation(value = "查询用户在门店下抵扣金列表")
	@RequestMapping(value = "/queryDeducteGoldList",method = {RequestMethod. POST ,RequestMethod. GET})
	public ResponseVo<Map<String,Object>> queryDeducteGoldList(
			@ApiParam(required = true, name = "userId", value = "入参为用户id")
			@RequestParam(value = "userId", required = true) Integer userId,
			@ApiParam(required = true, name = "storeId", value = "门店id")
			@RequestParam(value = "storeId", required = true) Integer storeId) throws Exception {
	
		logger.info("msg1=开始查询用户在门店下抵扣金列表,,method=queryDeducteGoldList,,userId={},,storeId={}",userId,storeId);
		
		if(userId==null){
			ResponseVo<Map<String,Object>> resVo=new ResponseVo<Map<String,Object>>();
			resVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resVo;
		}
		
		if(storeId==null){
			ResponseVo<Map<String,Object>> resVo=new ResponseVo<Map<String,Object>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		Map<String,Object> resultMap=new HashMap<String,Object>();
		
		BigDecimal balanceTotal=null;
		//查询用户在门店下可以抵扣金总额
		Map<String,Object> ddfBillMap=new HashMap<String,Object>();
		ddfBillMap.put("storeId", storeId);
		ddfBillMap.put("userId", userId);
		List<SystemStoreDdfBill> storeDdfBillList=storeDdfBillService.getEffectiveStoreDdfBillList(ddfBillMap);
		
		List<DeducteGoldVo> listDeducteGoldVo=null;
		
		if(storeDdfBillList!=null && !storeDdfBillList.isEmpty()){
			listDeducteGoldVo=new ArrayList<DeducteGoldVo>();
			Double blaceTotalValue=0.00d;
			for(SystemStoreDdfBill storeDdfBill:storeDdfBillList){
				//发放总金额
				BigDecimal totalAmount=storeDdfBill.getTotalAmount();
				//已使用总金额
				BigDecimal usedAmount=storeDdfBill.getUsedAmount();
				//余额
				BigDecimal balance=storeDdfBill.getBalance();
				Date effectTimeEnd=storeDdfBill.getEffectTimeEnd();
				//防止 使用总额大于发放总额
				if(totalAmount.doubleValue()>usedAmount.doubleValue()){
					Double balanceValue=balance.doubleValue();
					blaceTotalValue=ArithUtil.add(blaceTotalValue, balanceValue);
					DeducteGoldVo deducteGoldVo=new DeducteGoldVo();
					deducteGoldVo.setBalance(String.valueOf(balance.doubleValue()));
					
					String effectEndTime=DateUtil.formatTime(effectTimeEnd);
					deducteGoldVo.setEffectTimeEnd(effectEndTime);
					listDeducteGoldVo.add(deducteGoldVo);
				}
			}
			balanceTotal=new BigDecimal(blaceTotalValue);
			//设置返回值
			DecimalFormat df = new DecimalFormat("0.00"); 
			String totalAmount = df.format(balanceTotal.doubleValue());
			
			resultMap.put("totalAmount", totalAmount);
			resultMap.put("deducteGoldList", listDeducteGoldVo);
		}else{
			balanceTotal=new BigDecimal(0);
			//设置返回值
			DecimalFormat df = new DecimalFormat("0.00"); 
			String totalAmount = df.format(balanceTotal.doubleValue());
			resultMap.put("totalAmount", totalAmount);
			resultMap.put("deducteGoldList", listDeducteGoldVo);
		}
		ResponseVo<Map<String,Object>> res = new ResponseVo<Map<String,Object>>();
		res.setResult(resultMap);
		logger.info("msg1=结束查询用户在门店下抵扣金列表,,method=queryDeducteGoldList,,userId={},,storeId={}",userId,storeId);
		return res;
	}
	

}